<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { type FormInstance, type FormRules } from "element-plus"
import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue"
import { getLoginCodeApi } from "@/api/login"
import { type LoginRequestData } from "@/api/login/types/login"
import { useFocus } from "./hooks/useFocus"
import {getTokenApi} from "@/api/gptLogin"
import ForgetCode from "./components/ForgetCode.vue"

const router = useRouter()
const { isFocus, handleBlur, handleFocus } = useFocus()

/** 登录表单元素的引用 */
const loginFormRef = ref<FormInstance | null>(null)

/** 登录按钮 Loading */
const loading = ref(false)

/** 登录表单数据 */
// 百昌 admin 12345678
const loginFormData: LoginRequestData = reactive({
  tenantName: "百昌",
  userName: "admin",
  password: "RrwK@6Y7"
})
// 记住密码
const remember = ref(false)

/** 登录表单校验规则 */
const loginFormRules: FormRules = {
  tenantName: [{ required: true, message: "请输入公司名称", trigger: "blur" }],
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }]
}
// 登录账号和密码是否正确
const accountError = ref(false)
const codeError = ref(false)

/** 登录逻辑 */
const handleLogin = () => {
  getTokenApi({grant_type:"client_credentials",client_id:"r3Aj2wrNd4IwTo5wDB7WOvtA",
  client_secret:"1eU8WcxQUzDKFw0nH7MgE4Y4iLBMgilU"
  }).then(res=>{
    console.log("res",res);
    router.push({ path: "/" })
  })
  // loginFormRef.value?.validate((valid: boolean, fields) => {
  //   if (valid) {
  //     if (remember.value) {
  //       // 记住密码
  //       localStorage.setItem("TENANT_NAME", loginFormData.tenantName)
  //       localStorage.setItem("USER_NAME", loginFormData.userName)
  //       localStorage.setItem("PASSWORD", loginFormData.password)
  //       localStorage.setItem("REMEMBER", "true")
  //     } else {
  //       localStorage.setItem("TENANT_NAME", "")
  //       localStorage.setItem("USER_NAME", "")
  //       localStorage.setItem("PASSWORD", "")
  //       localStorage.setItem("REMEMBER", "false")
  //     }
  //     loading.value = true
  //     useUserStore()
  //       .login(loginFormData)
  //       .then((code) => {
  //         if (code === "909") {
  //           // 账号到期
  //           overdueRef.value.visible = true
  //         } else if (code === "910") {
  //           // 账号禁用
  //           forbidRef.value.visible = true
  //         } else {
  //           router.push({ path: "/" })
  //         }
  //       })
  //       .catch(() => {
  //         // loginFormData.password = ""
  //       })
  //       .finally(() => {
  //         loading.value = false
  //       })
  //   } else {
  //     console.error("表单校验不通过", fields)
  //   }
  // })
}

// 弹框
const forgetRef = ref(null)
const overdueRef = ref(null)
const forbidRef = ref(null)
// 忘记密码 去掉
// const forgetHandle = () => {
//   forgetRef.value.visible = true;
// }
// 账号禁用

// 账号到期

// 如果记住密码了，获取账户密码
onMounted(() => {
  if (localStorage.getItem("REMEMBER") === "true") {
    loginFormData.tenantName = localStorage.getItem("TENANT_NAME")
    loginFormData.userName = localStorage.getItem("USER_NAME")
    loginFormData.password = localStorage.getItem("PASSWORD")
    remember.value = true
  }
})
</script>

<template>
  <div class="login-container">
    <div class="login-bg flex">
      <div class="login-card">
        <div class="title flex-between">
          <img src="@/assets/login/recheck_logo.png" style="width: 121px; height: 51px" />
          <!-- <img src="@/assets/login/sifa_logo.png" style="width: 161px; height: 59px" /> -->
          <img src="@/assets/login/bc_logo.png" style="width: 185px; height: 37px; margin-left: 20px" />
        </div>
        <div class="login-title">
          <h1>登录</h1>
          <span>Login to access your account</span>
        </div>
        <div class="content">
          <el-form ref="loginFormRef" :model="loginFormData" :rules="loginFormRules" @keyup.enter="handleLogin">
            <el-form-item prop="tenantName">
              <el-input
                class="custom"
                v-model.trim="loginFormData.tenantName"
                placeholder="请输入公司名称"
                type="text"
                tabindex="1"
                size="large"
              >
                <template #prefix>
                  <img src="../../assets/login/company_name_icon.svg" />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="userName">
              <el-input
                class="custom"
                v-model.trim="loginFormData.userName"
                placeholder="请输入用户名"
                type="text"
                tabindex="1"
                size="large"
              >
                <template #prefix>
                  <img src="../../assets/login/user_icon.svg" />
                </template>
              </el-input>
              <div class="error-tip" v-if="accountError">帐号不存在，请重新输入!</div>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                class="custom"
                v-model.trim="loginFormData.password"
                placeholder="请输入密码"
                type="password"
                tabindex="2"
                size="large"
                show-password
                @blur="handleBlur"
                @focus="handleFocus"
              >
                <template #prefix>
                  <img src="../../assets/login/code_icon.svg" />
                </template>
              </el-input>
              <div class="error-tip" v-if="codeError">密码不正确，请重新输入!</div>
            </el-form-item>
            <el-form-item>
              <div class="flex-between forget">
                <el-checkbox class="custom" v-model="remember" label="记住密码" size="large" />
                <!-- <span class="text" @click="forgetHandle">忘记密码？</span> -->
              </div>
            </el-form-item>
            <el-button
              class="logoin-button"
              round
              :loading="loading"
              type="primary"
              size="large"
              @click.prevent="handleLogin"
              >立即登录</el-button
            >
          </el-form>
        </div>
      </div>
      <div class="describe">
        <div class="text">对公财产线索查询&分析&处置</div>
        <div class="text">一体化平台</div>
        <img src="../../assets/login/login_bg4.png" class="img" />
      </div>
    </div>
    <div class="case-code">
      <!-- ©2024 BCZCC.COM.All rights reserved.   -->
      <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">沪ICP备2021026343号-5</a>
    </div>
    <!-- dialog -->
    <ForgetCode
      ref="overdueRef"
      :title="`${loginFormData.tenantName}，您好！`"
      text="您的帐号已到期，如需继续使用请联系平台客服"
    />
    <!-- <ForgetCode ref="forgetRef" title="忘记密码" text="如您忘记密码，请联系0400-83849"></ForgetCode> -->
    <ForgetCode
      ref="forbidRef"
      :title="`${loginFormData.tenantName}，您好！`"
      text="您的帐号已被禁用，如需继续使用请联系提供给您帐号的管理员或联系平台客服。"
    />
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  background:
    url("../../assets/login/login_bg1.png") no-repeat left bottom/745px 563px,
    url("../../assets/login/login_bg2.png") no-repeat right top/461px 441px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;
  .login-bg {
    width: 1200px;
    height: 560px;
    background: url("../../assets/login/login_bg3.png") no-repeat right top/720px 600px;
    border-radius: 20px;
    overflow: hidden;
  }
  .describe {
    padding: 80px 68px;
    height: 100%;
    box-sizing: border-box;

    .text {
      color: #f8f8f8;
      font-size: 32px;
      font-style: normal;
      line-height: 50px;
    }
    .img {
      margin-top: 32px;
      width: 318.308px;
      height: 198.522px;
    }
  }
  .theme-switch {
    position: fixed;
    top: 5%;
    right: 5%;
    cursor: pointer;
  }
  .login-card {
    width: 480px;
    height: 100%;
    max-width: 90%;
    padding: 21px 56px;
    box-sizing: border-box;
    background-color: var(--el-bg-color);
    overflow: hidden;
    .title {
      display: flex;
      justify-content: left;
      align-items: end;
      height: 100px;
      padding-bottom: 15px;
      img {
        height: 100%;
      }
    }
    .login-title {
      h1 {
        color: #160a4a;
        font-size: 30px;
        line-height: 51px;
        font-weight: normal;
        margin: 0;
      }
      span {
        color: #636363;
        font-size: 18px;
        font-weight: 500;
        line-height: 26px;
      }
    }
    .content {
      padding: 20px 50px 0 0;
      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 40px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }
      .el-button.logoin-button {
        width: 215px;
        margin-top: 10px;
        font-size: 18px;
      }
      .el-button--primary.logoin-button {
        --el-button-bg-color: #160a4a;
      }
      .el-button.logoin-button:hover {
        background-color: #160a4a;
        border-color: #160a4a;
      }
    }
  }
}
.el-input.custom {
  font-size: 18px;
  border-bottom: 1px solid #d2d2d2;
  :deep(.el-input__wrapper) {
    box-shadow: none;
  }
  &::placeholder {
    color: #bbb;
    font-size: 18px;
  }
}
.forget {
  width: 100%;
  font-size: 15.6px;
  :deep(.el-checkbox.custom) {
    color: #440979;
    --el-checkbox-checked-text-color: #440979;
    .el-checkbox__input.is-checked .el-checkbox__inner {
      background-color: #440979;
      border-color: #440979;
    }
  }
  .text {
    color: #999;
    cursor: pointer;
  }
}
.error-tip {
  color: #f00;
  font-size: 12px;
}
.case-code {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding-top: 20px;
  font-size: 12px;
  z-index: 100;
  a {
    color: #29185a;
    &:hover {
      text-decoration: underline;
    }
  }
}
</style>
